{{/* Table of Contents */}}

{{- $toc := .Params.toc | default true -}}

{{/* UI Text */}}
{{- $on_this_page := T "on_this_page" }}
{{- $edit_this_page := T "edit_page" }}

<nav class="hb-toc order-last hidden w-64 shrink-0 xl:block print:hidden px-4" aria-label="table of contents">
  {{ partial "functions/get_hook" (dict "hook" "toc-start" "context" .) }}
  {{- if $toc }}
  <div class="hb-scrollbar text-sm [hyphens:auto] sticky top-16 overflow-y-auto pr-4 pt-6 max-h-[calc(100vh-var(--navbar-height)-env(safe-area-inset-bottom))] -mr-4 rtl:-ml-4">
    {{- with .Fragments.Headings -}}
    <p class="mb-4 font-semibold tracking-tight">{{ $on_this_page }}</p>
    {{- range . -}}
    <ul>
      {{- with .Headings -}}{{ template "toc-headings" (dict "headings" . "level" 0) }}{{- end -}}
    </ul>
    {{- end -}}
    {{- end -}}

    {{- $class := "" -}}
    {{- if .Fragments.Headings -}}
      {{- $class = "mt-8 border-t dark:border-neutral-700 pt-8" -}}
    {{- end -}}

    {{/* Page Features */}}
    {{- if site.Params.features.repository.url | and .Params.editable -}}
    <div class="{{ $class }} sticky bottom-0 flex flex-col items-start gap-2 pb-8">

        {{ $edit_url := site.Params.features.repository.url | default "" }}
        {{ $branch := site.Params.features.repository.branch | default "main" }}
        {{ $dir := site.Params.features.repository.content_dir | default "content" }}
        {{- with .File -}}{{ $edit_url = urls.JoinPath $edit_url "edit" $branch $dir (replace .Path "\\" "/") }}{{- end -}}
        {{- with .Params.edit_url -}}{{ $edit_url = . }}{{- end -}}
        <a class="text-xs font-medium text-gray-500 hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-100 contrast-more:text-gray-800 contrast-more:dark:text-gray-50" href="{{ $edit_url }}" target="_blank" rel="noreferer">{{ $edit_this_page }}</a>

    </div>
    {{- end -}}

    {{ partial "components/backlinks" . }}

    {{ partial "functions/get_hook" (dict "hook" "toc-end" "context" .) }}

  </div>
  {{ end -}}
</nav>

{{/* Render Headings */}}
{{- define "toc-headings" -}}
  {{- $headings := .headings -}}
  {{- $level := .level -}}
  {{- if ge $level 4 -}}
    {{ return }}
  {{- end -}}

  {{- $padding := (mul $level 4) -}}{{/* Requires inclusion in Tailwind safelist!! */}}
  {{- $class := cond (eq $level 0) "font-semibold" (printf "pl-%d rtl:pr-%d" $padding $padding) -}}

  {{- range $headings }}
    {{- if .Title }}
      <li class="my-2 scroll-my-6 scroll-py-6">
        <a class="{{ $class }} inline-block text-gray-500 hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-300 w-full break-words" href="#{{ anchorize .ID }}">
          {{- .Title | plainify | htmlUnescape -}}
        </a>
      </li>
    {{- end -}}

    {{- with .Headings -}}
      {{ template "toc-headings" (dict "headings" . "level" (add $level 1)) }}
    {{- end -}}

  {{- end -}}
{{- end -}}
